$("document").ready(function(){
    $("#index").addClass("active");
	//设置历史搜索与搜索框同宽
	$(".history-search").width($(".search-content").width()+24);
	//左侧导航和显示广告同高
	$(".left-nav-detail").height($(".right").height());
	$(".left-nav-detail").css("left",$(".list-nav").width()+12);
	//指向左侧导航出现内容
	showNavMsg(".list-nav li:eq(0)","#nav-detail1");
	showNavMsg(".list-nav li:eq(1)","#nav-detail2");
	showNavMsg(".list-nav li:eq(2)","#nav-detail3");
	showNavMsg(".list-nav li:eq(3)","#nav-detail4");
	showNavMsg(".list-nav li:eq(4)","#nav-detail5");
	showNavMsg(".list-nav li:eq(5)","#nav-detail6");
	showNavMsg(".list-nav li:eq(6)","#nav-detail7");
	//轮播图js代码
	var container=document.getElementById('takePhoto');
    var list=document.getElementById('list');
    var buttons=document.getElementById('buttons').getElementsByTagName('span');
    var last=document.getElementById('last');
    var next=document.getElementById('next');
    var index=1;
    var animated=false;
    var timer;
    function showButton(){
        for (var i = 0; i < buttons.length; i++) {
            if (buttons[i].className=='on') {
                buttons[i].className='';2
                break;
            }
        }
        buttons[index-1].className='on';
    }
    //左右箭头按钮切换图片
    function animate(offset) {
        animated=true;
        var newLeft=parseInt(list.style.left)+offset;
        var time=300;//位移总时间
        var interval=10;//位移间隔时间
        var speed=offset/(time/interval);//位移速度

        function go(){
            if ((speed<0 && parseInt(list.style.left)>newLeft) || (speed>0 && parseInt(list.style.left)<newLeft)) {
                list.style.left=parseInt(list.style.left)+speed+'px';
                setTimeout(go,interval);
            } else {
                animated=false;
                list.style.left=newLeft+'px';
                if (newLeft>-600) {
                    list.style.left=-(600 * parseInt($("#addNum").val()))+'px';
                }
                if (newLeft<-(600 * parseInt($("#addNum").val()))) {
                    list.style.left=-600+'px';
                }
            }
        }
        go();
    }
    //自动轮播
    function play() {
        timer=setInterval(function(){
            next.onclick();
        } ,3000);
    }
    //清除计时器
    function stop() {
        clearInterval(timer);
    }
    //左按钮事件
    last.onclick=function(){
        if (index==1) {
            index=parseInt($("#addNum").val());
        } else {
            index-=1;
        }
        showButton();
        if (animated==false) {
            animate(600);  
        }
    }
    //右按钮事件
    next.onclick=function(){
        if (index==parseInt($("#addNum").val())) {
            index=1;
        } else {
            index+=1;
        }
        showButton();
        if (animated==false) {
            animate(-600);  
        }
        
    }
    container.onmouseover=stop;
    container.onmouseout=play;
    play();

    //加载更多数据
    $(".more").click(function(){

    });
});
//左侧导航显示封装函数
function showNavMsg(item,box){
	$(item).mouseover(function(){
		$(box).addClass("left-nav-detail-show");
		$(item).css("border-right","0");
	});
	$(item).mouseout(function(){
		$(box).removeClass("left-nav-detail-show");
		$(item).removeClass("lihover");
		$(item).css("border-right","1px solid #ccc");
	})
	$(box).mouseover(function(){
		$(item).addClass("lihover");
		$(item).css("border-right","0");
		$(box).addClass("left-nav-detail-show");
	});
	$(box).mouseout(function(){
		$(item).removeClass("lihover");
		$(item).css("border-right","1px solid #ccc");
		$(box).removeClass("left-nav-detail-show");
	});
}
//加载更多数据的封装函数?不清楚怎么写
function addMoreData(){
    var p = 2;
    if($("#page").val() != null && $("#page").val() != ""){
        p = $("#page").val();
    }
    var result = '';
    $.ajax({
        type:'get',
        url:'/index/addMoreData.action?p='+p,
        dataType:"json",
        beforeSend:function () {
            $("#addMore").empty();
            $("#addMore").html("<img height='30' width='30' src='/image/loading.gif'/>");
            $("#addMore").addClass("disabled");
        },
        success:function(data){
            if (data.status === "Bad_Sql") {
                sweetAlert("WARNING","没有更多数据了","warning");
            }else if (data.status === "Get_Data_Rows"){
                sweetAlert("WARNING","没有更多数据了","warning");
            }else if (data.status === "Get_More_Data_Fail"){
                sweetAlert("WARNING","没有更多数据了","warning");
            }else if (data.status === "Success") {
                for (var i in data.userList) {
                    result += '<div class="col-sm-3 store-div">'
                        + '<a href="'
                        + '/search/toDetail.action?user_id='+data.userList[i].user_id
                        + '&p=1"><img class="store-img" src="/upload/business/'
                        + data.userList[i].user_img
                        + '"></a>'
                        + '<h3>'
                        + '<a href="/search/toDetail.action?user_id='+data.userList[i].user_id
                        +'&p=1">'
                        + data.userList[i].user_name
                        + '</a>'
                        + '<a href="/search/toDetail.action?user_id='+data.userList[i].user_id
                        +'&p=1"><span>'
                        + data.userList[i].user_evaluate_num
                        + '</span>人评价</a>'
                        + '</h3>'
                        + '<p>'
                        + '<span class="price-avg">人均￥<strong>'
                        + data.userList[i].user_avarage_price
                        + '</strong></span>'
                        + '</p>'
                        + '</div>';
                }
                $("#store-main").append(result);
                $("#page").val(data.page.currentPage + 1);
                if (data.page.totalRows < data.page.endIndex){
                    sweetAlert("Success","数据加载完毕","success");
                }
            }else {
                sweetAlert("WARNING","没有更多数据了","warning");
            }
        },
        complete:function (data) {
            $("#addMore").empty();
            $("#addMore").html("加载更多");
            $("#addMore").removeClass("disabled");
        },
        error:function(jqXHR){
            $("#addMore").empty();
            $("#addMore").html("加载更多");
            $("#addMore").removeClass("disabled");
            sweetAlert("ERROR","错误码"+jqXHR.status,"error");
        }
    });
}

function toDetail(id) {

}